package com.ltkj.ticket.service.impl;

import java.util.List;

import com.ltkj.common.core.exception.ServiceException;
import com.ltkj.common.core.utils.DateUtils;
import com.ltkj.common.core.utils.DateUtils;
import com.ltkj.common.core.web.domain.AjaxResult;
import com.ltkj.common.redis.service.RedisService;
import com.ltkj.common.security.utils.SecurityUtils;
import com.ltkj.ticket.domain.dto.AppletTicketDetailQueryDTO;
import com.ltkj.ticket.domain.dto.AppletTicketQueryDTO;
import com.ltkj.ticket.domain.vo.*;
import com.ltkj.ticket.mapper.AppletTicketMapper;
import com.ltkj.ticket.mapper.TicActivityStockMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ltkj.ticket.mapper.TicActivityBusinessMapper;
import com.ltkj.ticket.domain.TicActivityBusiness;
import com.ltkj.ticket.service.ITicActivityBusinessService;

/**
 * 活动票Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-31
 */
@Service
public class TicActivityBusinessServiceImpl implements ITicActivityBusinessService 
{
    private static final Logger log = LoggerFactory.getLogger(TicActivityBusinessServiceImpl.class);
    @Autowired
    private TicActivityBusinessMapper ticActivityBusinessMapper;
    @Autowired
    private AppletTicketMapper appletTicketMapper;
    @Autowired
    private TicActivityStockMapper ticActivityStockMapper;
    @Autowired
    private RedisService redisService;
    @Value("${ticket.activity}")
    private Integer activityLimit;

    /**
     * 查询活动票
     * 
     * @param activityId 活动票主键
     * @return 活动票
     */
    @Override
    public TicActivityBusiness selectTicActivityBusinessByActivityId(Long activityId)
    {
        return ticActivityBusinessMapper.selectTicActivityBusinessByActivityId(activityId);
    }

    /**
     * 查询活动票列表
     * 
     * @param ticActivityBusiness 活动票
     * @return 活动票
     */
    @Override
    public List<TicActivityBusiness> selectTicActivityBusinessList(TicActivityBusiness ticActivityBusiness)
    {
        return ticActivityBusinessMapper.selectTicActivityBusinessList(ticActivityBusiness);
    }

    /**
     * 新增活动票
     * 
     * @param ticActivityBusiness 活动票
     * @return 结果
     */
    @Override
    public int insertTicActivityBusiness(TicActivityBusiness ticActivityBusiness)
    {
        try {
            ticActivityBusiness.setIsDel("0");
            ticActivityBusiness.setCreateBy(SecurityUtils.getUsername());
            ticActivityBusiness.setCreateTime(DateUtils.getNowDate());
            //根据当前时间判断票的状态
            if(DateUtils.getNowDate().after(ticActivityBusiness.getActivityTimeStart())&&
                    DateUtils.getNowDate().before(ticActivityBusiness.getActivityTimeEnd())){
                ticActivityBusiness.setScenicStatus("2");
            }
            if (DateUtils.getNowDate().after(ticActivityBusiness.getActivityTimeEnd())){
                ticActivityBusiness.setScenicStatus("3");
            }
            if (DateUtils.getNowDate().before(ticActivityBusiness.getActivityTimeStart())){
                ticActivityBusiness.setScenicStatus("1");
            }
            ticActivityBusinessMapper.insertTicActivityBusiness(ticActivityBusiness);
            //查询小程序列表
            AppletTicketQueryDTO queryDTO = new AppletTicketQueryDTO();
            queryDTO.setTicketType("2");
            List<AppletTicketPageVo> pageList = appletTicketMapper.list(queryDTO);
            //向redis中缓存景区门票列表
            if (redisService.hasKey("PAGE:page_tic_act")) {
                redisService.setCacheList("PAGE:page_tic_act", pageList);
            }

        }catch (Exception e){
            e.printStackTrace();
            throw new ServiceException("新增景区门票异常");
        }
        return 200;
    }

    /**
     * 修改活动票
     * 
     * @param ticActivityBusiness 活动票
     * @return 结果
     */
    @Override
    public int updateTicActivityBusiness(TicActivityBusiness ticActivityBusiness)
    {
        ticActivityBusiness.setUpdateBy(SecurityUtils.getUsername());
        ticActivityBusiness.setUpdateTime(DateUtils.getNowDate());
        ticActivityBusinessMapper.updateTicActivityBusiness(ticActivityBusiness);
        //查询小程序列表
        AppletTicketQueryDTO queryDTO = new AppletTicketQueryDTO();
        queryDTO.setTicketType("2");
        List<AppletTicketPageVo> pageList = appletTicketMapper.list(queryDTO);
        //向redis中缓存景区门票列表
        redisService.udpateCacheList("PAGE:page_tic_act", pageList);
        //更新后判断详情中是否有缓存有缓存更新详情redis
        if (redisService.hasKey("DETAIL:HD_"+ticActivityBusiness.getActivityId())) {
            AppletTicketDetailVo vo = new AppletTicketDetailVo();
            TicketDetailVo ticketDetailVo = ticActivityStockMapper.selectDetailById(ticActivityBusiness.getActivityId());
            vo.setTicketDetailVo(ticketDetailVo);
            //轮播图
            TicActivityBusiness business = ticActivityBusinessMapper.selectTicActivityBusinessByActivityId(ticActivityBusiness.getActivityId());
            List<String> pics = ticActivityStockMapper.selectTicketPics(business.getTicOtherId());
            vo.setTicketPics(pics);
            // 游览时间
            AppletTicketDetailQueryDTO detailQueryDTO = new AppletTicketDetailQueryDTO();
            detailQueryDTO.setTicketId(ticActivityBusiness.getActivityId());
            detailQueryDTO.setTicketActivityLimit(activityLimit);
            List<TicketTourVo> tourVos = ticActivityStockMapper.selectTourVos(detailQueryDTO);
            if (tourVos != null && tourVos.size() > 0) {
                for (TicketTourVo tourVo : tourVos) {
                    //获取每天票种信息
                    List<TicketTypeVo> list = ticActivityStockMapper.selectTictkeTypes(tourVo.getStockDate(), ticActivityBusiness.getActivityId());
                    tourVo.setVoList(list);
                }
            }
            vo.setTourTime(tourVos);
            redisService.updateCacheObject("DETAIL:HD_" + ticActivityBusiness.getActivityId(), vo);
        }
        return 200;
    }

    /**
     * 批量删除活动票
     * 
     * @param activityIds 需要删除的活动票主键
     * @return 结果
     */
    @Override
    public int deleteTicActivityBusinessByActivityIds(Long[] activityIds)
    {
        ticActivityBusinessMapper.deleteTicActivityBusinessByActivityIds(activityIds);
        //查询小程序列表
        AppletTicketQueryDTO queryDTO = new AppletTicketQueryDTO();
        queryDTO.setTicketType("2");
        List<AppletTicketPageVo> pageList = appletTicketMapper.list(queryDTO);
        //向redis中缓存景区门票列表
        redisService.udpateCacheList("PAGE:page_tic_act", pageList);
        return 200;
    }

    /**
     * 删除活动票信息
     * 
     * @param activityId 活动票主键
     * @return 结果
     */
    @Override
    public int deleteTicActivityBusinessByActivityId(Long activityId)
    {
        return ticActivityBusinessMapper.deleteTicActivityBusinessByActivityId(activityId);
    }

    @Override
    public AjaxResult offshelf(Long activityId) {
        ticActivityBusinessMapper.offshelf(activityId);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult onshelf(Long activityId) {
        ticActivityBusinessMapper.onshelf(activityId);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult select() {
        TicActivityBusiness business = new TicActivityBusiness();
        business.setIsDel("0");
        List<TicActivityBusiness> list = ticActivityBusinessMapper.selectTicActivityBusinessList(business);
        return AjaxResult.success(list);
    }
}
